package com.tanhua.admin.controller;


import com.tanhua.admin.service.ManageService;
import com.tanhua.model.db.Freeze;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/manage")
@RestController
public class ManageController {

    @Autowired
    private ManageService manageService;


    /*
     *  用户数据翻页
     *    请求方式和请求链接 : GET  /users
     *    请求参数: Query pagesize  page
     *    返回值: PageResult  List <UserInfo>
     * */
    @GetMapping("/users")
    public ResponseEntity users(@RequestParam(defaultValue = "10") Integer pagesize,
                                @RequestParam(defaultValue = "1") Integer page) {

        PageResult vo = manageService.users(pagesize, page);
        return ResponseEntity.ok(vo);
    }


    /*
     *  用户基本资料
     *    请求方式和请求链接 :  GET   /users/:userID
     *    请求参数: userID
     *    返回值:  USERINFOVO
     * */
    @GetMapping("/users/{id}")
    public ResponseEntity findById(@PathVariable("id") Long id) {

        UserInfo userInfo = manageService.findById(id);
        return ResponseEntity.ok(userInfo);
    }

    /*
     * 动态分页
     *    请求方式和请求链接 : GET  /messages
     *    请求参数: Query: pagesize page uid state
     *    返回值:  PageResult  List < MovementsVo>
     * */

    @GetMapping("/messages")
    public ResponseEntity messages(@RequestParam(defaultValue = "10") Integer pagesize,
                                   @RequestParam(defaultValue = "1") Integer page,
                                   Long uid,Integer state){

        System.out.println("++++++++++++++++++messages"+uid);
        PageResult result=manageService.messages(pagesize,page,uid,state);
        return  ResponseEntity.ok(result);
    }

    /*
     *  动态详情
     *    请求方式和请求链接 : GET  /messages/{id}
     *    请求参数: 动态id
     *    返回值:MovementsVo
     * */

    @GetMapping("/messages/{id}")
    public ResponseEntity findMovement(@PathVariable("id") String id){

        System.out.println(id);
        MovementsVo vo=manageService.findMovement(id);
        return ResponseEntity.ok(vo);

    }


    /**
     *视频记录翻页
     *  请求方式和请求链接 : GET  /videos
     *  请求参数: Query : pagesize page uid
     *  返回值: PageResult  List < VideoVo>
     */
    @GetMapping("/videos")
    public ResponseEntity videos(@RequestParam(defaultValue = "10") Integer pagesize,
                                 @RequestParam(defaultValue = "1") Integer page,
                                 Long uid){

        System.out.println("++++++++++++++++++"+uid);
      PageResult result=  manageService.videos(pagesize,page,uid);

      return ResponseEntity.ok(result);
    }
    /**
     * 接口名称：用户冻结操作
     *  接口路径：POST   /users/freeze
     *  请求参数：Body
     *      userId: 用户id    integer
     *      freezingTime:   冻结时间，1为冻结3天，2为冻结7天，3为永久冻结   integer
     *      freezingRange:  冻结范围，1为冻结登录，2为冻结发言，3为冻结发布动态 integer
     *      reasonsForFreezing: 冻结原因    string
     *      frozenRemarks:  冻结备注    string
     *  返回数据:
     *      message:操作结果   string
     */
    @PostMapping("/users/freeze")
    public ResponseEntity freeze(@RequestBody Freeze freeze){
        //解析请求数据
//        Integer userId = (Integer) map.get("userId");
//        Integer freezingTime = (Integer) map.get("freezingTime");
//        Integer freezingRange = (Integer) map.get("freezingRange");
//        String reasonsForFreezing = (String) map.get("reasonsForFreezing");
//        String frozenRemarks = (String) map.get("frozenRemarks");
        String message = manageService.freeze(freeze);
        return ResponseEntity.ok(message);
    }

    /**
     * 动态审核通过 :
     * 请求链接 : POST/manage/messages/pass
     * 请求参数 : body : String[]
     * 返回值 : String message
     */
    @PostMapping("/messages/pass")
    public ResponseEntity pass(@RequestBody String[] ids) {
        System.out.println(ids[0]);
        String message = manageService.pass(ids);
        Map map = new HashMap();
        map.put("message", message);
        return ResponseEntity.ok(map);
    }

    /**
     * 动态审核拒绝 :
     *     请求连接 : POST/manage/messages/reject
     *     请求参数 : body : String[]
     *     返回值 : String message
     */
    @PostMapping("/messages/reject")
    public ResponseEntity reject(@RequestBody String[] ids){
        String message = manageService.reject(ids);
        Map map = new HashMap();
        map.put("message",message);
        return ResponseEntity.ok(map);
    }

    /**
     * 用户解冻操作
     *  接口路径：POST  /users/unfreeze
     *  请求参数 :
     *      userId : 用户id
     *      reasonsForThawing : 解冻原因
     */
    @PostMapping("/users/unfreeze")
    public ResponseEntity unfreeze(@RequestBody Map map){
        //解析请求参数
        Integer userId = (Integer) map.get("userId");
        String reasonsForThawing = (String) map.get("reasonsForThawing");
        String message = manageService.unfreeze(userId,reasonsForThawing);
        return ResponseEntity.ok(message);
    }
}
